home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / mc220.zip / TEXTNUM.C < prev    next >
C/C++ Source or Header  |  1992-02-24  |  2KB  |  74 lines

  1. #include \mc\stdio.h
  2.  
  3. /*
  4.  * Main program which processes all of its arguments, interpreting each
  5.  * one as a numeric value, and displaying that value as english text.
  6.  */
  7. main(argc, argv)
  8.     int argc;
  9.     char *argv[];
  10. {
  11.     int i;
  12.  
  13.     if(argc < 2)                /* No arguments given */
  14.         abort("\nUse: textnum <value*>\n");
  15.  
  16.     for(i=1; i < argc; ++i) {    /* Display all arguments */
  17.         textnum(atoi(argv[i]));
  18.         putc('\n', stdout); }
  19. }
  20.  
  21. /*
  22.  * Text tables and associated function to display an unsigned integer
  23.  * value as a string of words. Note the use of RECURSION to display
  24.  * the number of thousands and hundreds.
  25.  */
  26.  
  27. /* Table of single digits and teens */
  28. char *digits[] = {
  29.     "Zero", "One", "Two", "Three", "Four", "Five", "Six",
  30.     "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve",
  31.     "Thirteen", "Fourteen", "Fifteen", "Sixteen",
  32.     "Seventeen", "Eighteen", "Nineteen" };
  33.  
  34. /* Table of tens prefix's */
  35. char *tens[] = {
  36.     "Ten", "Twenty", "Thirty", "Fourty", "Fifty",
  37.     "Sixty", "Seventy", "Eighty", "Ninety" };
  38.  
  39. /* Function to display number as string */
  40. textnum(value)
  41.     unsigned value;
  42. {
  43.     char join_flag;
  44.  
  45.     join_flag = 0;
  46.  
  47.     if(value >= 1000) {                /* Display thousands */
  48.         textnum(value/1000);
  49.         fputs(" Thousand", stdout);
  50.         if(!(value %= 1000))
  51.             return;
  52.         join_flag = 1; }
  53.  
  54.     if(value >= 100) {                /* Display hundreds */
  55.         if(join_flag)
  56.             fputs(", ", stdout);
  57.         textnum(value/100);
  58.         fputs(" Hundred", stdout);
  59.         if(!(value %= 100))
  60.             return;
  61.         join_flag = 1; }
  62.  
  63.     if(join_flag)                    /* Separator if required */
  64.         fputs(" and ", stdout);
  65.  
  66.     if(value > 19) {                /* Display tens */
  67.         fputs(tens[(value/10)-1], stdout);
  68.         if(!(value %= 10))
  69.             return;
  70.         putc(' ', stdout); }
  71.  
  72.     fputs(digits[value], stdout);    /* Display digits */
  73. }
  74.